<template>
  <div class="singer_song">
    <div class="list">
      <div
        @dblclick="startSong(item)"
        :class="{ active_position: item.id === songId }"
        v-for="item in songList.slice(0, 16)"
        :key="item.id"
        class="item"
      >
        <div class="left">
          <el-image
            lazy
            style="width: 35px; height: 35px; border-radius: 5px"
            :src="item.al.picUrl"
            fit="cover"
          ></el-image>
        </div>
        <div class="right">
          <div class="music_name">{{ item.name }}</div>
          <span
            v-for="(item1, index1) in item.ar"
            :key="index1"
            class="singer_name"
            >{{ item1.name }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getSearchList } from "@/util/findMusic.js";
import { mapGetters } from "vuex";

export default {
  name: "Singersong",

  data() {
    return {
      songList: [],
    };
  },

  created() {
    this.getSearchList(1);
  },
    computed: {
    ...mapGetters("musicPlay", [
      //当前播放歌曲id
      "songId",
      //是否在播放
      "isPlaying",
      //历史播放列表
      "hasPlayList",
      //当前播放歌曲url
      "musicUrl",
    ]),
  },

  methods: {
    //获取歌单歌曲列表
    async getSearchList(page) {
      let {
        data: { result },
      } = await getSearchList(
        this.$route.params.keyword,
        100,
        (page - 1) * 24,
        1
      );
      if (result) {
        this.songList = result.songs;
      }
    },
    //播放歌曲
    async startSong(musicDetail) {
      //保存当前播放列表
      this.$store.dispatch("musicPlay/savePlayList", this.songList);
      sessionStorage.setItem("playList", JSON.stringify(this.songList));

      //保存播放状态
      this.$store.dispatch("musicPlay/saveIsPlaying", "true");

      //保存当前歌曲id
      this.$store.dispatch("musicPlay/saveSongId", musicDetail.id);
      sessionStorage.setItem("songId", musicDetail.id);
    },
  },
};
</script>

<style lang="less" scoped>
.singer_song {
  // padding: 0 8rem;
  .list {
    //   padding: 7px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    align-items: center;
    .item {
      cursor: pointer;
      display: flex;
      align-items: center;
      width: 21%;
      background-color: #fff;
      padding: 5px;
      border-radius: 10px;
      margin-top: 10px;
      &:hover {
        background-color: #f2f2f4;
      }

      .left {
      }
      .right {
        margin-left: 13px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        .music_name {
          font-size: 16px;
          font-weight: 600;
        }
        .singer_name {
          color: rgb(100, 89, 89);
          font-size: 12px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }
    .active_position {
      color: #335eea;
      background: #eaeffd;
      .right {
        div,
        span {
          color: #335eea;
        }
        .singer_name {
          span {
            color: #335eea;
          }
        }
      }
    }
  }
}
</style>